package com.xwj.back.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xwj.back.mapper.OrderMapper;
import com.xwj.back.mapper.StoreMapper;
import com.xwj.back.pojo.OrderPay;
import com.xwj.back.pojo.Store;
import com.xwj.back.service.OrderService;
import com.xwj.back.utils.listener.DataListener;
import com.xwj.back.utils.listener.OrderDataListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.*;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private StoreMapper storeMapper;

    private static final String ROLE_ADMIN = "ROLE_ADMIN";


    @Override
    public List<OrderPay> getOrders(String username, String role) {
        if (ROLE_ADMIN.equals(role)) {
            return orderMapper.selectList(null);
        }
        QueryWrapper<OrderPay> orderQueryWrapper = new QueryWrapper<>();
        orderQueryWrapper.inSql("store_id", "select id from store where boss_name = '" + username + "'");
        return orderMapper.selectList(orderQueryWrapper);
    }

    @Override
    public String addOrder(Map<String, String> data) {
        Integer storeId = Integer.valueOf(data.get("storeId"));
        double utilities = Double.parseDouble(data.get("utilities"));
        double hireFees = Double.parseDouble(data.get("hireFees"));
        double allFees = utilities + hireFees;
        String orderNumber = UUID.randomUUID().toString().substring(0,6);
        QueryWrapper<Store> storeQueryWrapper = new QueryWrapper<>();
        storeQueryWrapper.eq("id", storeId);
        Store store = storeMapper.selectOne(storeQueryWrapper);
        orderMapper.insert(new OrderPay(null, storeId, store.getName(),
                utilities, hireFees, allFees, new Date(), orderNumber, false));
        return "success";
    }

    @Override
    public String deleteOrder(Map<String, String> data) {
        Integer orderId = Integer.valueOf(data.get("orderId"));
        orderMapper.deleteById(orderId);
        return "success";
    }

    @Override
    public String updateOrder(Map<String, String> data) {
        Integer storeId = Integer.valueOf(data.get("storeId"));
        Integer orderId = Integer.valueOf(data.get("orderId"));
        double utilities = Double.parseDouble(data.get("utilities"));
        double hireFees = Double.parseDouble(data.get("hireFees"));
        UpdateWrapper<OrderPay> wrapper = new UpdateWrapper<>();
        wrapper.set("store_id", storeId)
                .set("utilities", utilities)
                .set("hire_fees", hireFees)
                .set("all_fees", utilities + utilities).eq("order_id", orderId);
        orderMapper.update(null, wrapper);
        return "success";
    }

    @Override
    public List<OrderPay> getOrdersByStatus(boolean status) {
        QueryWrapper<OrderPay> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", status);
        return orderMapper.selectList(queryWrapper);
    }

    @Override
    public Map<String, String> ImportExcel(MultipartFile file) throws IOException {
        EasyExcel.read(file.getInputStream(),OrderPay.class,new OrderDataListener(orderMapper)).sheet().doRead();
        Map<String, String> map = new HashMap<>();
        map.put("message","success");
        return map;
    }


}
